To implement Dijkstra's algorithm efficiently, we use a Min-Heap structure, provided by Python's built-in `heapq` module, to manage the Priority Queue.

  • The core mechanism maps directly to the conceptual steps: initialization, loop, extraction, and relaxation.
  • We initialize the distances dictionary with $\infty$ for all nodes and $0$ for the starting node.
  • The Priority Queue (PQ) stores tuples of `(distance, node)` to ensure the node with the smallest distance is always popped first.
  • A common optimization checks if the extracted distance is greater than the currently recorded distance, implicitly handling stale entries in the PQ.
  • The relaxation step updates a neighbor's distance only if a shorter path is found through the current node.
  • The overall time complexity is typically $O(V \log V + E \log V)$ when using a binary heap.